/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.youmoula.mtsq.components

import android.util.Log
import android.widget.ImageView
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilledIconButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.IconButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.LiveData
import coil.compose.rememberAsyncImagePainter
import com.squareup.picasso.Picasso
import com.youmoula.mtsq.R
import com.youmoula.mtsq.ReplyHomeUIState
import com.youmoula.mtsq.data.Email
import com.youmoula.mtsq.data.Recipe
import kotlinx.coroutines.flow.MutableStateFlow
import java.util.Objects

@Composable
fun ReplySearchBar(modifier: Modifier = Modifier,
                   searchKey:String,
                   //imageUrl:String,
                   searchChange:(String)->Unit,
) {
    //var text:String = searchKey.orEmpty()
    //var textx by rememberSaveable { mutableStateOf(searchKey.orEmpty()) }
    Row(
        modifier = modifier
            .fillMaxWidth()
            .padding(16.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Icon(
            imageVector = Icons.Default.Search,
            contentDescription = stringResource(id = R.string.search),
            modifier = Modifier.padding(start = 16.dp),
        )
//        Text(
//            text = stringResource(id = R.string.search),
//            modifier = Modifier
//                .weight(1f)
//                .padding(16.dp),
//            style = MaterialTheme.typography.bodyMedium
//        )

        TextField(
            value = searchKey,
            onValueChange = searchChange,// {uiState.searchKey =it;  Log.d("search key", replyHomeUIState.searchKey.orEmpty()) },
            placeholder = { Text("菜的做法") }
        )

//        OutlinedTextField(text, enabled = true, readOnly = false,
//            onValueChange = { text = it }, )

        ReplyProfileImage(
            //drawableResource = R.mipmap.fooda,
            drawableResource = R.mipmap.fooda.toString(),
            drawableResourceInt = R.mipmap.fooda,
            description = stringResource(id = R.string.app_name_cn),
            modifier = Modifier
                .padding(12.dp)
                .size(32.dp)
        )
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun EmailDetailAppBar(
    email: Recipe?,
    isFullScreen: Boolean,
    modifier: Modifier = Modifier,
    onBackPressed: () -> Unit
) {
    TopAppBar(
        modifier = modifier,
        title = {
            Column(
                modifier = Modifier.fillMaxWidth(),
                horizontalAlignment = if (isFullScreen) Alignment.CenterHorizontally
                else Alignment.Start
            ) {
                Text(
                    text = Objects.toString(email?.recipeTitle, ""),
                    style = MaterialTheme.typography.titleMedium,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )
                Text(
                    modifier = Modifier.padding(top = 4.dp),
                    text = "${email?.createAt} ${stringResource(id = R.string.search)}",
                    style = MaterialTheme.typography.labelMedium,
                    color = MaterialTheme.colorScheme.outline,
                )
            }
        },
        navigationIcon = {
            if (isFullScreen) {
                FilledIconButton(
                    onClick = onBackPressed,
                    modifier = Modifier.padding(8.dp),
                    colors = IconButtonDefaults.filledIconButtonColors(
                        containerColor = MaterialTheme.colorScheme.primary,
                        contentColor = MaterialTheme.colorScheme.onPrimary
                    )
                ) {
                    Icon(
                        imageVector = Icons.Default.ArrowBack,
                        contentDescription = stringResource(id = R.string.search),
                        modifier = Modifier.size(14.dp)
                    )
                }
            }
        },
        actions = {
            IconButton(
                onClick = {

                   // Log.d("IconButton Click", "search")
                /*Click Implementation*/ },
            ) {

                Icon(
                    painter = rememberAsyncImagePainter(email?.recipeImage.toString().replace("\"","")) ,
                    contentDescription = stringResource(id = R.string.search),
                    tint = MaterialTheme.colorScheme.onSecondary
                )
            }
        }
    )
}
